﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam
{
    public partial class FormChiTiet : Form
    {
        private string ma;
        private DateTime dt;
        private int maloai;
        private CXulyPhieuNhap xuly_phieunhap;
        private CXulyHoaDon xuly_hoadon;
        private CXulythongtinsach xuly_sach;
        private CXulyPhieuXuat xuly_phieuxuat;

        public FormChiTiet(string ma, string date, int loai)
        {
            InitializeComponent();
            this.ma = ma;
            dt = DateTime.Parse(date);
            maloai = loai;
        }

        private void FormView_Load(object sender, EventArgs e)
        {
            xuly_phieunhap = new CXulyPhieuNhap();
            xuly_hoadon= new CXulyHoaDon();
            xuly_sach = new CXulythongtinsach();
            xuly_phieuxuat = new CXulyPhieuXuat();

            if (maloai == 1)
            {
                type1();
                foreach (PhieuNhap x in xuly_phieunhap.getDsPhieuNhap())
                    if (x.PhieuNhapDate.Value.Month == dt.Month && x.PhieuNhapDate.Value.Year == dt.Year)
                        foreach (ChiTietPhieuNhap ctpn in x.ChiTietPhieuNhaps)
                            if (ctpn.Sach.SachNo == ma)
                                HienThiChiTietNhap(x);
            }
            if (maloai == 2)
            {
                type2();
                foreach (HoaDon hd in xuly_hoadon.getdsHoaDon())
                    if (hd.HoaDonDate.Value.Month == dt.Month && hd.HoaDonDate.Value.Year == dt.Year)
                        foreach (ChiTietHoaDon cthd in hd.ChiTietHoaDons)
                            if (cthd.Sach.SachNo == ma)
                                HienThiChiTietBan(hd);
            }
            if (maloai == 3)
            {
                type3();
                foreach (PhieuXuat px in xuly_phieuxuat.getDSPhieuXuat())
                    if (px.PhieuXuatDate.Value.Month == dt.Month && px.PhieuXuatDate.Value.Year == dt.Year)
                        foreach (ChiTietPhieuXuat ctpx in px.ChiTietPhieuXuats)
                            if (ctpx.Sach.SachNo == ma)
                                HienThiChiTietXuat(px);
            }
            if (maloai == 4)
            {
                type4();
                foreach (HoaDon hd in xuly_hoadon.getdsHoaDon())
                    if (hd.HoaDonNo == ma && hd.HoaDonDate.Value.Month == dt.Month && hd.HoaDonDate.Value.Year == dt.Year)
                        HienThiChiTietHoaDon(hd);
            }

            if (maloai == 5)
            {
                type5();
                foreach (PhieuNhap pn in xuly_phieunhap.getDsPhieuNhap())
                    if (pn.PhieuNhapNo == ma && pn.PhieuNhapDate.Value.Month == dt.Month && pn.PhieuNhapDate.Value.Year == dt.Year)
                        HienThiChiTietPhieuNhap(pn);
            }
        }

        private void HienThiChiTietNhap(PhieuNhap pn)
        {
            ListViewItem lvi = lv.Items.Add(pn.PhieuNhapNo);
            lvi.SubItems.Add(pn.PhieuNhapDate.Value.ToShortDateString());
            foreach (ChiTietPhieuNhap ctpn in pn.ChiTietPhieuNhaps)
                if (ctpn.Sach.SachNo == ma)
                {
                    lvi.SubItems.Add(ctpn.ChiTietPhieuNhapQuantum.ToString());
                    lvi.SubItems.Add(ctpn.ChiTietPhieuNhapPrice.ToString());
                }
        }

        private void HienThiChiTietBan(HoaDon hd)
        {
            ListViewItem lvi = lv.Items.Add(hd.HoaDonNo);
            lvi.SubItems.Add(hd.HoaDonDate.Value.ToShortDateString());
            foreach(ChiTietHoaDon cthd in hd.ChiTietHoaDons)
                if (cthd.Sach.SachNo == ma)
                    lvi.SubItems.Add(cthd.ChiTietHoaDonQuantum.ToString());
            foreach (Sach s in xuly_sach.getdsSach())
                if (s.SachNo == ma)
                    lvi.SubItems.Add(s.SachPrice.ToString()); 
        }

        private void HienThiChiTietHoaDon(HoaDon hd)
        {
            foreach (ChiTietHoaDon cthd in hd.ChiTietHoaDons)
            {
                ListViewItem lvi = lv.Items.Add(cthd.Sach.SachNo);
                lvi.SubItems.Add(cthd.Sach.SachName);
                lvi.SubItems.Add(cthd.Sach.NhaXuatBan.NhaXuatBanName);
                lvi.SubItems.Add(cthd.ChiTietHoaDonQuantum.ToString());
                lvi.SubItems.Add(cthd.Sach.SachPrice.ToString());
            }
        }

        private void HienThiChiTietXuat(PhieuXuat px)
        {
            ListViewItem lvi = lv.Items.Add(px.PhieuXuatNo);
            lvi.SubItems.Add(px.PhieuXuatDate.Value.ToShortDateString());
            foreach (ChiTietPhieuXuat ctpx in px.ChiTietPhieuXuats)
                if (ctpx.Sach.SachNo == ma)
                    lvi.SubItems.Add(ctpx.Quantum.ToString());
        }

        private void HienThiChiTietPhieuNhap(PhieuNhap pn)
        {
            foreach (ChiTietPhieuNhap ctpn in pn.ChiTietPhieuNhaps)
            {
                string s ="";
                ListViewItem lvi = lv.Items.Add(ctpn.Sach.SachNo);
                lvi.SubItems.Add(ctpn.Sach.SachName);
                foreach (Sach_NhaCungCap sncc in ctpn.Sach.Sach_NhaCungCaps)
                    s += sncc.NhaCungCap.NhaCungCapName + " ";
                lvi.SubItems.Add(s);
                lvi.SubItems.Add(ctpn.ChiTietPhieuNhapQuantum.ToString());
                lvi.SubItems.Add(ctpn.ChiTietPhieuNhapPrice.ToString());
            }
        }

        private void type1()
        {
            lv.Clear();
            lv.Columns.Add("Mã Phiếu Lập", 90);
            lv.Columns.Add("Ngày Lập", 100, HorizontalAlignment.Right);
            lv.Columns.Add("Số Lượng", 60, HorizontalAlignment.Right);
            lv.Columns.Add("Giá", 100, HorizontalAlignment.Right);
        }

        private void type2()
        {
            lv.Clear();
            lv.Columns.Add("Mã Hóa Đơn", 90);
            lv.Columns.Add("Ngày Lập", 100, HorizontalAlignment.Right);
            lv.Columns.Add("Số Lượng", 60, HorizontalAlignment.Right);
            lv.Columns.Add("Giá", 100, HorizontalAlignment.Right);
        }

        private void type3()
        {
            lv.Clear();
            lv.Columns.Add("Mã Phiếu Xuất", 90);
            lv.Columns.Add("Ngày Lập", 100, HorizontalAlignment.Right);
            lv.Columns.Add("Số Lượng", 60, HorizontalAlignment.Right);
        }

        private void type4()
        {
            lv.Clear();
            lv.Columns.Add("Mã Sách", 90);
            lv.Columns.Add("Tên Sách", 150);
            lv.Columns.Add("Nhà Xuất Bản", 130);
            lv.Columns.Add("Số Lượng", 60, HorizontalAlignment.Right);
            lv.Columns.Add("Giá", 100, HorizontalAlignment.Right);
        }

        private void type5()
        {
            lv.Clear();
            lv.Columns.Add("Mã Sách", 90);
            lv.Columns.Add("Tên Sách", 150);
            lv.Columns.Add("Nhà Cung Cấp", 130);
            lv.Columns.Add("Số Lượng", 60, HorizontalAlignment.Right);
            lv.Columns.Add("Giá", 100, HorizontalAlignment.Right);
        }
    }
}
